<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
        Application["MemOnline"] = 0;




        int counterValue = 75381; int iSoSanh = 1;
        string counterFile = Server.MapPath("visited.txt");
        string anGianFile = Server.MapPath("tempVisited.txt");

        if (System.IO.File.Exists(anGianFile))
        {
            using (System.IO.StreamReader sr = new System.IO.StreamReader(anGianFile))
            {
                string result = sr.ReadLine();
                if (result != null)
                {
                    iSoSanh = Convert.ToInt32(result);
                }
            }
        }
        if (System.IO.File.Exists(counterFile))
        {
            using (System.IO.StreamReader sr = new System.IO.StreamReader(counterFile))
            {
                string result = sr.ReadLine();
                if (result != null)
                {
                    //counterValue = Convert.ToInt32(result);
                }
            }
        }
        Application["MemVisited"] = counterValue;
        if (counterValue<iSoSanh)
            Application["MemVisited"] = iSoSanh;
        else
            Application["MemVisited"] = counterValue;

    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }
        
    void Application_Error(object sender, EventArgs e) 
    {

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started        
        Application.Lock();
        Application["MemOnline"] = (int)Application["MemOnline"] + 1;
        Application["MemVisited"] = (int)Application["MemVisited"] + 1;



        string counterFile = Server.MapPath("visited.txt");
        System.IO.StreamWriter sw = System.IO.File.CreateText(counterFile);
        sw.WriteLine(Application["MemVisited"].ToString());
        sw.Close();





        Application.UnLock();

    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.
        Application.Lock();
        Application["MemOnline"] = (int)Application["MemOnline"] - 1;
        Application.UnLock();

    }
       
</script>
